package com.zjhhsd.blackJack.controller;

import com.zjhhsd.blackJack.util.CardUtil;
import com.zjhhsd.blackJack.util.RoomUtil;
import com.zjhhsd.blackJack.mapper.UserMapper;
import com.zjhhsd.blackJack.model.RoomInfo;
import com.zjhhsd.blackJack.model.User;
import com.zjhhsd.blackJack.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Map;

@Controller
public class LoginController {

    public static ArrayList<String> roomLog = RoomUtil.roomLog;

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/")
    public String defaultP(){
        return "redirect:/login";
    }

    @GetMapping("/login")
    public String tologin(
            @RequestParam(value = "plat", required = false)String plat,
            HttpServletRequest request
    ){
        if("plat".equals(plat)){
            return "plat";
        }
        //登录界面设置session的name为空
        request.getSession().setAttribute("name",null);
        //登录界面设置session的wrong值为空---判断用户登录是否成功
        request.getSession().setAttribute("wrong",null);
        return "login";
    }

    @PostMapping("/login")
    public String login(
            @RequestParam("name") String username,
            @RequestParam("password") String password,
            @RequestParam("ip") String ip,
            HttpServletResponse response,
            HttpServletRequest request, Model model
            ){
        User user = userMapper.verify(username,password);
        if (user != null){
            System.out.println("用户"+username+"登录成功，ip定位是：" + ip + " time：" +  System.currentTimeMillis());
            String place = ip.substring(ip.indexOf(",") + 1);
            roomLog.add("用户'" + username + "'在" + place + "登录");
            model.addAttribute("username",username);
            //初始化登录的用户信息
            UserInfo userInfo = new UserInfo();
            userInfo.setUsername(username);
            userInfo.setBoss(false);
            userInfo.setOpened(false);
            userInfo.setIdentity("等待中");
            userInfo.setHead("");
            RoomInfo roomInfo = MainController.getRoomInfo();
            Map<String, UserInfo> map = roomInfo.getMap();
            String bossName = roomInfo.getBossName();
            if("".equals(bossName)&& map.size() <= 1){
                //如果是房间内的第一个或唯一一个用户，则设置为庄家
                userInfo.setBoss(true);
                roomInfo.setBossName(username);
                roomLog.add("用户'" + username + "'成为庄家");
            }
            map.remove(username);
            if(roomInfo.getBossName().equals(username)){
                //如果玩家是庄家，那么重复登录后还应该是庄家，不应该被清除数据
                userInfo.setBoss(true);
            }
            map.put(username,userInfo);
            return "plat";
            // TODO: 2020/1/22
        }else{
            request.getSession().setAttribute("wrong","wrong");//添加一个session说明密码错误
            return "login";
        }
    }

    @GetMapping("/logout")
    public String logout( @RequestParam("username") String username,HttpServletRequest request,
                          HttpServletResponse response){
        RoomInfo roomInfo = MainController.getRoomInfo();
        Map<String, UserInfo> map = roomInfo.getMap();
        map.remove(username);
        if(roomInfo.getBossName().equals(username)){
            roomLog.add("庄家"+username+"退出，房间变更为准备状态");
            //如果庄家退出，则将庄家置空，房间设置为准备状态，
            roomInfo.setBossName("");
            roomInfo.setStatus("准备中");
            roomInfo.setDemandName("");
            CardUtil.clearUserCard(roomInfo);
            CardUtil.needClear = true;
        }else{
            roomLog.add("闲家"+username+"退出");
        }
        return "redirect:/";
    }

}
